﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WebMsg;

namespace MsgTester
{
    public partial class Client : Form
    {
        WebMsg.WebMsg smsClient;
        String creditBalance;

        public Client()
        {
            InitializeComponent();

            ddProvider.Focus();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            smsClient.QueueMessage(txtFrom.Text, txtTo.Text, txtMessage.Text);

            sendWorker.RunWorkerAsync();
        }

        private void txtMessage_TextChanged(object sender, EventArgs e)
        {
            lbLength.Text = txtMessage.Text.Length.ToString();
        }

        protected void saveCurrentCredentials()
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (config.AppSettings.Settings[ddProvider.Text + "_username"] != null)
            {
                config.AppSettings.Settings.Remove(ddProvider.Text + "_username");
            }

            if (config.AppSettings.Settings[ddProvider.Text + "_password"] != null)
            {
                config.AppSettings.Settings.Remove(ddProvider.Text + "_password");
            }

            config.AppSettings.Settings.Add(ddProvider.Text + "_username", txtUsername.Text);
            config.AppSettings.Settings.Add(ddProvider.Text + "_password", txtPassword.Text);

            config.Save(ConfigurationSaveMode.Modified);
        }

        private void txt_Leave(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(this.ddProvider.Text) &&
               !String.IsNullOrEmpty(this.txtUsername.Text) &&
               !String.IsNullOrEmpty(this.txtPassword.Text))
            {
                saveCurrentCredentials();

                try
                {
                    smsClient = WebMsg.WebMsg.CreateWebMsg(ddProvider.Text);
                    this.txtFrom.Enabled = smsClient.CustomFrom;
                }
                catch (ArgumentException ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }

                // Set credentials
                smsClient.SetCredentials(txtUsername.Text, txtPassword.Text);

                CheckBalance();
            }
        }

        private void CheckBalance()
        {
            if (!balanceWorker.IsBusy)
            {
                lblBalance.Text = @"Checking...";
                balanceWorker.RunWorkerAsync();
            }
        }

        private void ddProvider_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ConfigurationManager.AppSettings[ddProvider.Text + "_username"] != null)
            {
                txtUsername.Text = ConfigurationManager.AppSettings[ddProvider.Text + "_username"];
            }
            else
            {
                ClearForm();
            }

            if (ConfigurationManager.AppSettings[ddProvider.Text + "_password"] != null)
            {
                txtPassword.Text = ConfigurationManager.AppSettings[ddProvider.Text + "_password"];
            }
            else
            {
                ClearForm();
            }

            if (ConfigurationManager.AppSettings[ddProvider.Text + "_from"] != null)
            {
                txtFrom.Text = ConfigurationManager.AppSettings[ddProvider.Text + "_from"];
            }
            else
            {
                ClearForm();
            }

            txtUsername.Focus();
        }

        private void balanceWorker_DoWork(object sender, DoWorkEventArgs e)
        {
             creditBalance = smsClient.GetCreditsRemaining();
        }

        private void balanceWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            lblBalance.Text = creditBalance;
        }

        private void txtFrom_TextChanged(object sender, EventArgs e)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (config.AppSettings.Settings[ddProvider.Text + "_from"] != null)
            {
                config.AppSettings.Settings.Remove(ddProvider.Text + "_from");
            }

            config.AppSettings.Settings.Add(ddProvider.Text + "_from", txtFrom.Text);

            config.Save(ConfigurationSaveMode.Modified);
        }

        private void ClearForm()
        {
            txtUsername.Text = "";
            txtPassword.Text = "";
            txtFrom.Text = "";
            lblBalance.Text = "";
        }

        private void sendWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            smsClient.Send();
        }

        private void sendWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            MessageBox.Show("Message Sent!");
            CheckBalance();
        }
    }
}
